package com.qf.day05;

import java.lang.reflect.Array;
import java.util.Arrays;

public class UserM {
    User[] user;
    int count;
    public UserM() {
        user = new User[5];
    }

    /**
     * 注册
     * @param username
     * @param password
     * @return  true false
     */
    public boolean regist(String username,String password){
       boolean exist = checkUserName(username);
       if(exist){
           System.out.println("用户名已经被注册，用户名="+username);
           return false;
       }
        User user1 = new User(username, password);
       //判断数组长度是否够用
        if(count==user.length){
            //对原来的数组进行扩容
            User[] users1 = Arrays.copyOf(user, user.length * 2);
            user = users1;  //把userm对象的user属性指向创建的数组
        }
       user[count] = user1;
       count++;
        return true;
    }

    /**
     * 检查用户名在userManager对象的users数组中是否存在
     * @param username
     * @return  true重名 false没重名
     */
    public boolean checkUserName(String username) {
        if(count==0){
            return false;
        }
        for (int i = 0; i < count; i++) {
            if(username.equals(user[i].getUsername())){
                return true;  //重名了
            }
        }
        return false;
    }

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    public boolean login(String username,String password){
        //查找user数组中是否包含用户名和密码都一致的user对象
        for (int i = 0; i < count; i++) {
            if (user[i].getUsername().equals(username)&&user[i].getPassword().equals(password)) {
                return true;
            }
        }
        System.out.println("用户名或者密码错误,username="+username+"password="+password);
        return false;
    }

    /**
     * 打印userm信息
     */
    public void printInfo(){
        System.out.println("count="+count);
        for (int i = 0; i < count; i++) {
            System.out.println(user[i].getPassword());
        }
    }
}
